package com.jungle.client.control;

import com.jungle.bfi.domain.vo.Option;
import javafx.collections.ObservableList;
import javafx.util.StringConverter;

import java.util.Objects;

public class OptionConvertor extends StringConverter<String> {
    private final ObservableList<Option> options;

    public OptionConvertor(ObservableList<Option> options) {
        this.options = options;
    }

    @Override
    public String toString(String option) {
        if (option == null) return "";

        // 根据 value 查找对应的 name
        for (Option item : options) {
            if (Objects.equals(item.getValue(), option)) {
                return item.getName();
            }
        }
        return "";
    }

    @Override
    public String fromString(String name) {
        if (name == null || name.isEmpty()) return null;
        // 根据 name 查找对应的 value
        for (Option option : options) {
            if (Objects.equals(option.getName(), name)) {
                return option.getValue();
            }
        }
        return null;
    }
}
